package com.yitongmao.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;

import com.google.gson.Gson;
import com.yitongmao.AppConstancts;
import com.yitongmao.adapter.AddressListAdapter;
import com.yitongmao.bean.AddressListBean;
import com.yitongmao.bean.LoginHuancunBean;
import com.yitongmao.common.CallBackListener;
import com.yitongmao.core.cache.ACache;
import com.yitongmao.core.network.service.BaseService;
import com.yitongmao.util.SignUtil;
import com.yitongmao.util.ToastUtils;
import com.yitongmao.yitmfruit.R;

import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.TreeMap;

import butterknife.Bind;
import butterknife.ButterKnife;

import static com.yitongmao.yitmfruit.R.id.btn_back;

public class AddAddressActivity extends AppCompatActivity {
    @Bind(btn_back)
    Button btnBack;
    private ImageView addressimage;
    private ListView addressListView;
    private Button mAddAddress;
    private LoginHuancunBean loginHuancunBean;
    private AddressListAdapter addressListAdapter;
    private String userId;
    private int loginId1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        immerSion();
        setContentView(R.layout.activity_add_address);
        ButterKnife.bind(this);
        Gson gson=new Gson();
        String loginId = ACache.get(AddAddressActivity.this).getAsString("loginId");
        if (loginId!=null){
            loginHuancunBean = gson.fromJson(loginId, LoginHuancunBean.class);
            int id = loginHuancunBean.userInfo.id;
            loginId1 = loginHuancunBean.userInfo.loginId;
            userId = String.valueOf(id);
            addressList(userId);
        }
        Intent intent=getIntent();
        String id1 = intent.getStringExtra("id");
        if (id1!=null&&id1.equals("666")){
            addressList(userId);
        }
        intView();
        setOnClick();
    }
    private void setOnClick() {
        mAddAddress.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(AddAddressActivity.this, SetAddAddressActivity.class);
                startActivity(intent);
                finish();
            }
        });
        btnBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AddAddressActivity.this.finish();
            }
        });
    }
    private void intView() {
        addressimage = (ImageView) findViewById(R.id.addressimage);
        addressListView = (ListView) findViewById(R.id.addressListView);
        mAddAddress = (Button) findViewById(R.id.mAddAddress);
    }
    //沉浸式状态栏
    public void immerSion() {
        getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
                    | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);
            window.setNavigationBarColor(Color.BLACK);
        }
    }
    public void addressList(String userId) {
        Map params = new TreeMap();
        String timestamp = String.valueOf(System.currentTimeMillis());
        String nonce = String.valueOf(new Random().nextInt());
        params.put("userId", userId);
        params.put("timestamp", timestamp);
        params.put("nonce", nonce);
        params.put("signLogin",SignUtil.signLogin(nonce,timestamp));
        params.put("loginId",String.valueOf(loginId1));
        params.put("sign", SignUtil.sign(params)); //这里也需要时完整的url
        BaseService.getInstance().simplePostRequest(AppConstancts.Net.addressListUrl, params, new CallBackListener() {
            @Override
            public void onStart() {
            }
            @Override
            public void onSuccess(Object result) {
                String string = (String) result;
                Log.e("Gson", "" + string);
                Gson gson = new Gson();
                AddressListBean addressListBean = gson.fromJson(string, AddressListBean.class);
                List<AddressListBean.AddressBean> address = addressListBean.address;
                if (addressListBean.result.state.equals("1")) {
                    addressimage.setVisibility(View.GONE);
                    addressListView.setVisibility(View.VISIBLE);
                    addressListAdapter = new AddressListAdapter(AddAddressActivity.this, address);
                    addressListView.setAdapter(addressListAdapter);
                    addressListAdapter.notifyDataSetChanged();
                } else {
                    addressListView.setVisibility(View.GONE);
                    addressimage.setVisibility(View.VISIBLE);
                }
            }
            @Override
            public void onFail(Object result) {
                ToastUtils.showShort(AddAddressActivity.this, "失败");
            }
        });

    }

}
